<!--
 * @Author: wuzhen
 * @Date: 2019-12-04 17:56:54
 * @LastEditors  : wuzhen
 * @LastEditTime : 2020-01-11 16:26:04
 * @Description: 通知管理
 -->
<template>
  <Card style="width:100%" dis-hover>
    <!-- 搜索表头/表头按钮 -->
    <search-table :label-width="90" :search-props="searchProps" @handle-filter="handleFilter">
      <template v-slot:buttons>
        <Button type="error" @click="onDelete">批量删除</Button>
      </template>
    </search-table>
    <wz-table ref="table" :search-api="searchApi" :columns="columns" @on-selection-chang="onSelectionChang" />
  </Card>
</template>
<script>
import index from "./mixins";
import { getMessageRecordList, delMessageRecord } from "@/request/api";
export default {
  mixins: [index],
  data() {
    return {
      searchApi: getMessageRecordList,
      apiParams: {
        flipper: {
          offset: 0,
          limit: 15,
          sort: "createtime desc"
        },
        bean: {}
      },
      messageids: []
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取table数据
    getData() {
      this.$refs.table.getData(this.apiParams);
    },
    // 搜索
    handleFilter(params) {
      let apiParams = {};
      if (params.date && params.date.length && params.date[0]) {
        apiParams["createtime"] = {
          min: new Date(params.date[0] + " 00:00:00").getTime(),
          max: new Date(params.date[1] + " 23:59:59").getTime()
        };
      }
      apiParams = { ...apiParams, ...params };
      this.apiParams["bean"] = { ...apiParams };
      this.apiParams["bean"][this.type] = this.keyword;
      this.apiParams["flipper"] = {
        offset: 0,
        limit: 15,
        sort: "createtime desc"
      };
      this.getData();
    },
    // table选中
    onSelectionChang(e) {
      this.messageids = e.map(v => v.messageid);
    },
    // 删除
    onDelete() {
      if (!this.messageids.length) {
        this.$Message.warning("请选择要删除的记录");
        return;
      }
      this.$Modal.warning({
        title: "友情提示",
        content: "确定要删除记录？",
        closable: true,
        onOk: async () => {
          const res = await delMessageRecord({
            messageid: JSON.stringify(this.messageids)
          });
          if (res.code === 200) {
            this.$Message.success("操作成功");
            this.messageids = [];
            this.getData();
          }
        }
      });
    }
  }
};
</script>
